﻿using System.Collections.Generic;
using LBoL.Base;
using LBoL.ConfigData;
using LBoL.Core;
using LBoL.Core.Battle;
using LBoL.Core.Battle.BattleActions;
using LBoL.Core.Battle.Interactions;
using LBoL.EntityLib.Cards.Character.Cirno;
using LBoL.EntityLib.StatusEffects.Others;
using LBoLEntitySideloader.Attributes;
using ProbeMod.Cards.Template;
using LBoL.Core.Cards;
using System.Linq;
using System;
using LBoL.Base.Extensions;

namespace ProbeMod.Cards
{
    /// <summary>
    /// 水晶塔
    /// </summary>
    public sealed class ProbePylonDef : ProbeCardTemplate
    {
        public override CardConfig MakeConfig()
        {
            CardConfig config = GetCardDefaultConfig();

            config.Rarity = Rarity.Common;
            config.Type = CardType.Friend;
            config.TargetType = TargetType.Nobody;
            config.Colors = new List<ManaColor>() { ManaColor.Blue };

            config.Cost = new ManaGroup() { Any = 1, Blue = 1 };
            config.UpgradedCost = new ManaGroup() { Blue = 1 };

            config.Loyalty = 2;
            config.UpgradedLoyalty = 3;

            //  被动耐久变化
            config.PassiveCost = 1;
            config.UpgradedPassiveCost = 1;

            //  主动耐久变化
            config.ActiveCost = -2;
            config.UpgradedActiveCost = -2;

            //  大招消耗耐久.
            config.UltimateCost = -4;
            config.UpgradedUltimateCost = -4;

            config.Value1 = 1;  //  被动耐久变化
            config.Value2 = 2;  //  大招耐久变化

            config.RelativeEffects = new List<string>() { "ProbeBuildingKeywordSe" };
            config.UpgradedRelativeEffects = new List<string>() { "ProbeBuildingKeywordSe" };

            config.RelativeCards = new List<string>() { "ProbeZealotCharge" };
            config.UpgradedRelativeCards = new List<string>() { "ProbeZealotCharge" };

            config.Index = CardIndexGenerator.GetUniqueIndex(config);
            return config;
        }
    }

    [EntityLogic(typeof(ProbePylonDef))]
    public sealed class ProbePylon : ProbeFriendCard
    {

        public override IEnumerable<BattleAction> OnDraw()
        {
            return EnterHandReactor();
        }

        private IEnumerable<BattleAction> EnterHandReactor()
        {
            Loyalty += 1;
            yield return null;

        }

        /// <summary>
        /// 被动：+1
        /// </summary>
        /// <returns></returns>
        public override IEnumerable<BattleAction> GetPassiveActions()
        {
            //Triigger the effect only if the card has been summoned. 
            if (!base.Summoned || base.Battle.BattleShouldEnd)
            {
                yield break;
            }
            base.NotifyActivating();
            //Increase base loyalty.
            base.Loyalty += base.PassiveCost;
            yield break;
        }

        /// <summary>
        /// 主动 -2 手牌同伴牌+1耐久,抽一张牌
        /// 终结 -4 手牌同伴牌+2耐久,将2张“狂热者冲锋”置入手牌
        /// </summary>
        /// <param name="selector"></param>
        /// <param name="consumingMana"></param>
        /// <param name="precondition"></param>
        /// <returns></returns>
        protected override IEnumerable<BattleAction> Actions(UnitSelector selector, ManaGroup consumingMana, Interaction precondition)
        {
            if (precondition == null || ((MiniSelectCardInteraction)precondition).SelectedCard.FriendToken == FriendToken.Active)
            {
                base.Loyalty += base.ActiveCost;
                yield return new DrawCardAction();
                foreach (Card item in base.Battle.HandZone.Where((Card card) => card.CardType == CardType.Friend))
                {
                    item.NotifyActivating();
                    item.Loyalty = item.Loyalty + Value1;
                }


                yield break;
            }
            base.Loyalty += base.UltimateCost;
            base.UltimateUsed = true;

            yield return new AddCardsToHandAction(Library.CreateCards<ProbeZealotCharge>(2));
            foreach (Card item in base.Battle.HandZone.Where((Card card) => card.CardType == CardType.Friend))
            {
                item.NotifyActivating();
                item.Loyalty = item.Loyalty + Value2;
            }

        }
    }
}
